import { createRouter, createWebHistory } from 'vue-router';
import Layout from '../layout/index.vue'
import { getCookie, removeCookie, setCookie } from 'typescript-cookie';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [
        {
          path: '/dashboard',
          component: () => import('../views/dashboard/index.vue'),
          meta: { title: '主页', icon: 'house' },
        }
      ],
    },
    {
      path: '/user',
      component: Layout,
      redirect: '/student',
      meta: { title: '用户管理', icon: 'user' },
      children: [
        {
          path: '/student/list',
          component: () => import('../views/user/student-list.vue'),
          meta: { title: '学生列表' },
        },
        {
          path: '/admin/list',
          component: () => import('../views/user/admin-list.vue'),
          meta: { title: '管理员列表' },
        },
        {
          path: '/student/edit',
          component: () => import('../views/user/student-edit.vue'),
          meta: { title: '学生创编', hidden: true }
        },
        {
          path: '/admin/edit',
          component: () => import('../views/user/admin-edit.vue'),
          meta: { title: '管理员创编', hidden: true }
        }
      ],
    },
    {
      path: '/exam',
      component: Layout,
      redirect: "/paper",
      meta: { title: "卷题管理", icon: 'Tickets' },
      children: [
        {
          path: '/paper',
          component: () => import("../views/exam/paperView.vue"),
          meta: { title: "试卷管理" },
        },
        {
          path: '/question',
          component: () => import("../views/exam/questionView.vue"),
          meta: { title: "题目管理" },
        },
      ]
    },
    {
      path: '/task',
      component: Layout,
      redirect: "/task",
      meta: { title: "任务管理", icon: 'Reading' },
      children: [
        {
          path: '/task/list',
          component: () => import("../views/task/taskList.vue"),
          meta: { title: "任务列表" },
        },
        {
          path: '/task/edit',
          component: () => import("../views/task/taskEdit.vue"),
          meta: { title: "任务创建" },
        },
      ]
    },
    {
      path: '/education',
      component: Layout,
      redirect: "/subject",
      meta: { title: "教育管理", icon: 'Collection' },
      children: [
        {
          path: '/subject',
          component: () => import("../views/education/subject.vue"),
          meta: { title: "学科管理" },
        },
        {
          path: '/subject/edit',
          component: () => import("../views/education/subject-edit.vue"),
          meta: { title: "学科编辑", hidden: true },
        },
      ]
    },
    {
      path: '/login',
      component: () => import('../views/login/index.vue'),
      meta: { hidden: true },
    },
    {
      path: '/score',
      component: Layout,
      redirect: '/score/student/list',
      meta: { title: '成绩管理', icon: 'ChatLineSquare' },
      children: [
        {
          path: '/score/student/list',
          component: () => import('../views/score/Answerlist.vue'),
          meta: { title: '答卷列表' },
        },

      ],
    },
    {
      path: '/message',
      component: Layout,
      redirect: '/message/student/list',
      meta: { title: '消息中心', icon: 'message' },
      children: [
        {
          path: '/message/student/list',
          component: () => import('../views/message/Messagelist.vue'),
          meta: { title: '消息列表' },
        },
        {
          path: '/message/admin/list',
          component: () => import('../views/message/Messagesent.vue'),
          meta: { title: '消息发送' },
        },
      ],
    },
    {
      path: '/log',
      component: Layout,
      redirect: '/log/student/list',
      meta: { title: '日志中心', icon: 'Document' },
      children: [
        {
          path: '/log/student/list',
          component: () => import('../views/log/userlog.vue'),
          meta: { title: '用户日志' },
        },
      ],
    },
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 是否登录
  if (getCookie('JSESSIONID')) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
